using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Drawing;
using System.Globalization;

namespace Camalon.ColorPicker.Design
{

	[Serializable]
	[TypeConverter(typeof(TransparentColorConverter))]
	[Editor(typeof(TransparentColorEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public struct TransparentColor
	{
		//tx = 1- (a/255)	==> a = 255 --> tx = 0d ==> 0% 
		//					==> a = 0   --> tx = 1d ==> 100%
				
		//a = 255-(tx*255)  ==> tx = 100%	= 1d --> a = 0
		//					==> tx = 0%		= 0d --> a = 255

		private Color   _color		;



		public TransparentColor( Color color )
		{
			_color = color;
		}


		[Browsable(true)]
		[DefaultValue( typeof( Color ), "Empty")]
		[TypeConverter(typeof(ExtendedColorConverter))]
		[Editor(typeof(TransparentColorEditor), typeof(System.Drawing.Design.UITypeEditor))]
		[RefreshProperties( RefreshProperties.All)]
		public Color Color
		{
			get	{	return _color;}
			set	{	_color = value;	}
		}

		[DefaultValue(0.0f)]
		[RefreshProperties( RefreshProperties.All)]
		[TypeConverter(typeof(RoundedPercentConverter))]
		[Description("Transparency value of color values range from 0% (totally opaque) to 100% (totally transparent).")]
		public float Transparency
		{
			get	{	return GetTransparency( _color.A );	}
		}


		[Browsable( false )]
		[Description ( "Indicates empty color.")] 
		public bool IsEmpty
		{
			get
			{
				return _color.IsEmpty;// _color.A ==0 && _color.R==0 && _color.G == 0 && _color.B==0;// == Color.Empty;
			}
		}


		[Browsable( false )]
		[Description ( "Indicates totally transparent color.")] 
		public bool IsTransparent
		{
			get
			{
				return !IsEmpty && _color.A == 0;
			}
		}


		[Browsable( false )]
		[Description ( "Indicates empty or totally transparent color.")] 
		public bool IsVisible
		{
			get{ return !IsEmpty && !IsTransparent;}

		}



		public static float GetTransparency( int colorA )
		{
			if( colorA < 0) colorA = 0;
			if( colorA > 255) colorA = 255;

			return 1.0f-((float)colorA/255.0f);
		}

		public static TransparentColor Empty
		{
			get
			{
				return new TransparentColor( System.Drawing.Color.Empty );
			}
		}

		public static TransparentColor Transparent
		{
			get
			{
				return new TransparentColor( System.Drawing.Color.Transparent );
			}
		}






		#region overrides
		public override bool Equals(object obj)
		{
			if(!( obj is TransparentColor)) return false;
			return this == (TransparentColor)obj;
		}

		public bool Equals(TransparentColor tranparentColor)
		{
			return _color == tranparentColor.Color;
		}

		public override int GetHashCode()
		{
			return _color.GetHashCode() ^ base.GetHashCode();
		}
		#endregion overrides 

		#region operatoren

		public static bool operator ==(	TransparentColor color1,TransparentColor color2	)
		{
			Color c1 = color1.Color;
			Color c2 = color2.Color;

			return c1.A == c2.A && c1.R == c2.R && c1.G == c2.G && c1.B == c2.B;
		}

		public static bool operator !=(	TransparentColor color1,TransparentColor color2	)
		{
			return !(color1 == color2);
		}

		public static bool operator ==(	Color color1,TransparentColor color2	)
		{
			TransparentColor c1 = color1;
			return c1 == color2;
		}

		public static bool operator !=(	Color color1,TransparentColor color2	)
		{
			TransparentColor c1 = color1;
			return !(c1 == color2);
		}

		public static bool operator ==(	TransparentColor color1,Color color2	)
		{
			return color1 == color2;
		}

		public static bool operator !=(	TransparentColor color1,Color color2	)
		{
			return !(color1 == color2);
		}

		public static implicit operator TransparentColor(	Color color )
		{
			return new TransparentColor(color);
		}

		public static implicit operator Color(	TransparentColor transparentColor )
		{
			return transparentColor.Color;
		}


		#endregion operatoren



	}


}
